home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / stars / line.c < prev    next >
Text File  |  1995-05-09  |  3KB  |  121 lines

  1. /*************************************************************************
  2.  *                                                                       *
  3.  *  Copyright (c) 1992, 1993 Ronald Joe Record                           *
  4.  *                                                                       *
  5.  *  All rights reserved. No part of this program or publication may be   *
  6.  *  reproduced, transmitted, transcribed, stored in a retrieval system,  *
  7.  *  or translated into any language or computer language, in any form or *
  8.  *  by any means, electronic, mechanical, magnetic, optical, chemical,   *
  9.  *  biological, or otherwise, without the prior written permission of:   *
  10.  *                                                                       *
  11.  *      Ronald Joe Record (408) 458-3718                                 *
  12.  *      212 Owen St., Santa Cruz, California 95062 USA                   *
  13.  *                                                                       *
  14.  *************************************************************************/
  15.  
  16. #define ABSOLUTE_VALUE(a)    (((a)<0) ? (0-(a)) : (a) )
  17.  
  18. typedef struct {
  19.     int x, y, u, v;
  20. } xy_t;
  21.  
  22. /*
  23.  * mv_line() takes a pair of points, a distance, and an xy_t pointer
  24.  * and returns the xy_t pointer pointing to a point whose coordinates
  25.  * are those along the line from 'p0' to 'p1' distance 'dir'. 
  26.  * The distance can be negative. 
  27.  */
  28.  
  29. xy_t *
  30. mv_line(p0, p1, dir, newpt)
  31. int dir;
  32. xy_t p0, p1;
  33. xy_t *newpt;
  34. {
  35.     xy_t delta;
  36.     xy_t *move_vert_line();
  37.     xy_t *move_horz_line();
  38.     xy_t *move_steep_line();
  39.     xy_t *move_shallow_line();
  40.  
  41.     delta.x = p0.x - p1.x;
  42.     delta.y = p0.y - p1.y;
  43.     if(0==delta.x)
  44.         move_vert_line(p0, p1, dir, newpt);
  45.     else if(0==delta.y)
  46.         move_horz_line(p0, p1, dir, newpt);
  47.     else if(ABSOLUTE_VALUE(delta.y) > ABSOLUTE_VALUE(delta.x))
  48.         move_steep_line(p0, p1, delta, dir, newpt);
  49.     else
  50.         move_shallow_line(p0, p1, delta, dir, newpt);
  51.     return(newpt);
  52. }
  53.  
  54. xy_t *
  55. move_steep_line(p0, p1, delta, dir, p2)
  56. int dir;
  57. xy_t p0, p1, delta;
  58. xy_t *p2;
  59. {
  60.     float slope;
  61.  
  62.     p2[0].x = p0.x;
  63.     p2[0].y = p0.y;
  64.     if(p0.y > p1.y)
  65.         p2[0].y -= dir;
  66.     else
  67.         p2[0].y += dir;
  68.     slope = (float)delta.y / (float)delta.x;
  69.     p2[0].x = (((float)(p2[0].y - p1.y))/slope) + (float)p1.x;
  70.     return(p2);
  71. }
  72.  
  73. xy_t *
  74. move_shallow_line(p0, p1, delta, dir, p2)
  75. int dir;
  76. xy_t p0, p1, delta;
  77. xy_t *p2;
  78. {
  79.     float slope;
  80.  
  81.     p2[0].x = p0.x;
  82.     p2[0].y = p0.y;
  83.     if(p0.x > p1.x)
  84.         p2[0].x -= dir;
  85.     else
  86.         p2[0].x += dir;
  87.     slope = (float)delta.y / (float)delta.x;
  88.     p2[0].y = (((float)(p2[0].x - p1.x)) * slope) + p1.y;
  89.     return(p2);
  90. }
  91.  
  92. xy_t *
  93. move_horz_line(p0, p1, dir, p2)
  94. int dir;
  95. xy_t p0, p1;
  96. xy_t *p2;
  97. {
  98.     p2[0].x = p0.x;
  99.     p2[0].y = p0.y;
  100.     if (p0.x > p1.x)
  101.         p2[0].x -= dir;
  102.     else
  103.         p2[0].x += dir;
  104.     return(p2);
  105. }
  106.  
  107. xy_t *
  108. move_vert_line(p0, p1, dir, p2)
  109. int dir;
  110. xy_t p0, p1;
  111. xy_t *p2;
  112. {
  113.     p2[0].x = p0.x;
  114.     p2[0].y = p0.y;
  115.     if (p0.y > p1.y)
  116.         p2[0].y -= dir;
  117.     else
  118.         p2[0].y += dir;
  119.     return(p2);
  120. }
  121.